<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class City extends CI_Controller {
	private $id;
	private $name;
	private $description;
	private $latitude;
	private $longitude;
	 
	public function __construct(){
		parent::__construct();
		$this->load->model('hotel_city_model');
	}
 
	/*
		load city list
	*/	
	public function index(){  
		 $data['page_html_title']      = "City List";
		 $data['main_content']    = 'city/list';
		 $data['hotel_city_list'] = $this->hotel_city_model->load_city_list();
		 $this->load->view('admin_layout',$data);
	}
	
	
	/*
		load a city to edit
	*/
	public function edit($city_id=0){ 
		 $data['page_html_title']   		= "City Edit";
		 $data['main_content'] 		= 'city/edit';
		 $data['selected_city_id']	= $city_id;
		 if($city_id>0){
			$data['city_data'] = $this->hotel_city_model->load_city_detail($city_id);
		 } 
		 $this->load->view('admin_layout',$data);
	}
		
	/*
		save a city
	*/	
	public function save(){ 
		$city_id = 0;
		   
		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'City Name', 'trim|required');
		$this->form_validation->set_rules('description', 'Description', 'trim|required');
		$this->form_validation->set_rules('latitude', 'Latitude', 'trim|required');
		$this->form_validation->set_rules('longitude', 'Longitude', 'trim|required');
		$this->form_validation->set_rules('distance_from_colombo', 'Distance from colombo', 'trim|required');

		if($this->form_validation->run()== FALSE)	{
			$data['page_html_title'] = "City Save Error";
			$data['main_content']    = 'city/edit'; 
			$city_id =($_POST['city_id']!=''?$_POST['city_id']:0); 
			$temp_data=array();
			foreach($_POST as $key=>$value){
				$temp_data[$key]=$value;
			}
			//$data['city_data']=$temp_data;
		}else{ 
			$result=$this->hotel_city_model->save();
			$data['page_html_title'] = "City Edit"; 
			$data['main_content'] 	 = 'city/edit'; 
			$data['message_id']		 = $result[1];
			$city_id 				 = $result[0];  
		} 
		if($city_id!=0){
			$data['city_data']    = $this->hotel_city_model->load_city_detail($city_id);
		}
		$data['selected_city_id'] = $city_id;
		$this->load->view('admin_layout',$data);
	}	

 	/*
		delete a specified city
	*/
	public function delete($city_id=0){
		 $this->load->helper('url');  
		 $data['message_id']   	  = $this->hotel_city_model->delete_city($city_id);
		 redirect('/admin/city/', 'refresh');	
	}
	
}
?>